package com.zc.manager.notify.controller;

import java.io.*;
import java.util.Base64;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Base64Utils;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.zc.manager.notify.domain.NotifyTemplate;
import com.zc.manager.notify.service.NotifyTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通知模版Controller
 *
 * @author wangjiabao
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/notify/template")
public class NotifyTemplateController extends BaseController {
    @Resource
    private NotifyTemplateService notifyTemplateService;

    /**
     * 查询通知模版列表
     */
    @PreAuthorize("@ss.hasPermi('notify:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(NotifyTemplate notifyTemplate) {
        startPage();
        List<NotifyTemplate> list = notifyTemplateService.selectNotifyTemplateList(notifyTemplate);
        return getDataTable(list);
    }

    /**
     * 无分页查询通知模版列表
     */
    @PreAuthorize("@ss.hasPermi('notify:template:list')")
    @GetMapping("/query")
    public TableDataInfo query(NotifyTemplate notifyTemplate) {
        List<NotifyTemplate> list = notifyTemplateService.selectNotifyTemplateList(notifyTemplate);
        return getDataTable(list);
    }

    /**
     * 导出通知模版列表
     */
    @PreAuthorize("@ss.hasPermi('notify:template:export')")
    @Log(title = "通知模版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NotifyTemplate notifyTemplate) {
        List<NotifyTemplate> list = notifyTemplateService.selectNotifyTemplateList(notifyTemplate);
        ExcelUtil<NotifyTemplate> util = new ExcelUtil<>(NotifyTemplate.class);
        util.exportExcel(response, list, "通知模版数据");
    }

    /**
     * 获取通知模版详细信息
     */
    @PreAuthorize("@ss.hasPermi('notify:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(notifyTemplateService.selectNotifyTemplateById(id));
    }

    /**
     * 新增通知模版
     */
    @PreAuthorize("@ss.hasPermi('notify:template:add')")
    @Log(title = "通知模版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NotifyTemplate notifyTemplate) {
        return toAjax(notifyTemplateService.insertNotifyTemplate(notifyTemplate));
    }

    /**
     * 修改通知模版
     */
    @PreAuthorize("@ss.hasPermi('notify:template:edit')")
    @Log(title = "通知模版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NotifyTemplate notifyTemplate) {
        return toAjax(notifyTemplateService.updateNotifyTemplate(notifyTemplate));
    }

    /**
     * 删除通知模版
     */
    @PreAuthorize("@ss.hasPermi('notify:template:remove')")
    @Log(title = "通知模版", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(notifyTemplateService.deleteNotifyTemplateByIds(ids));
    }

    @Log(title = "模板导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('notify:template:import')")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<NotifyTemplate> util = new ExcelUtil<NotifyTemplate>(NotifyTemplate.class);
        List<NotifyTemplate> notifyTemplateList = util.importExcel(file.getInputStream());
        return toAjax(notifyTemplateService.importNotifyTemplate(notifyTemplateList));
    }


}
